package com.wedo.mall.activity.user;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;

import com.e.library.activity.EActivity;
import com.e.library.http.EApi;
import com.e.library.http.EClient;
import com.e.library.http.EObserver;
import com.e.library.http.EResponse;
import com.e.library.utils.ESPUtils;
import com.e.library.utils.EUtils;
import com.wedo.mall.R;
import com.wedo.mall.activity.login.WLogin;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindString;
import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;

public class ChangePasswordActivity extends EActivity {

    @BindView(R.id.et_username) EditText et_username;
    @BindView(R.id.et_password) EditText et_password;
//    @BindView(R.id.et_check_code) EditText et_check_code;
//    @BindView(R.id.btn_check_code) Button btn_check_code;

    @BindString(R.string.register_vc_resend) String register_vc_resend;

    @Override
    protected int layout() {
        return R.layout.activity_change_password;
    }

    @Override
    protected void onActivityCreated(Bundle state) {

        tel();
    }

//    /**获取验证码*/
//    @OnClick(R.id.btn_check_code)
//    protected void onCheckCodeClick(){
//
////        String account = et_username.getText().toString();
////        if(!validation(account)) {
////            return;
////        }
//
//        String account = ESPUtils.getString(this, WLogin.USERNAME);
//        loading();
//        Observable<EResponse> observable = EClient.with().api().verifyCode(account,"2");
//        EClient.with().observer(observable, new EObserver() {
//            @Override
//            public void successful(EResponse response) {
//                mTimer.start();
//                dismiss(R.string.register_vc_send);
////                String code = response.string("code");
////                et_check_code.setText(code);
//            }
//
//            @Override
//            public void failure(String msg) {
//                dismiss(msg);
//            }
//        });
//
//    }

    @OnClick(R.id.btn_reset)
    protected void onResetClick(){

        String old_password = et_username.getText().toString();
        if (TextUtils.isEmpty(old_password)){
            toast(R.string.mp_old_password_hint);
            return;
        }

        if (old_password.length() <6 || old_password.length() >12){
            toast(R.string.register_password_hint);
            return;
        }

        String password = et_password.getText().toString();
        if (TextUtils.isEmpty(password)){
            toast(R.string.mp_new_password_hint);
            return;
        }

        if (password.length() <6 || password.length() >12){
            toast(R.string.register_password_hint);
            return;
        }

//        String code = et_check_code.getText().toString();
//        if (TextUtils.isEmpty(code)){
//            toast(R.string.register_vc_hint);
//            return;
//        }

        Map<String,String> params = new HashMap<>(5);
        params.put("old_password",old_password);
        params.put("password",password);
        params.put("method","app");
        //params.put("verify",code);

        loading();
        post(EApi.USER_RESETPASSWORD, params, new EObserver() {
            @Override
            public void successful(EResponse response) {
                dismiss(R.string.fp_reset_successfully);
                defaultFinish();
            }

            @Override
            public void failure(String msg) {
                dismiss(msg);
            }
        });


    }

//    /**验证修改账号*/
//    private boolean validation(String account){
//        if(TextUtils.isEmpty(account)) {
//            toast(R.string.fp_account_hint);
//            return false;
//        }
//
//        if (!EUtils.isMobile(account)){
//            if (!EUtils.isEmail(account)){
//                toast(R.string.fp_account_invalid);
//                return false;
//            }
//        }
//
//        return true;
//    }

//    private CountDownTimer mTimer = new CountDownTimer(60*1000,1000) {
//        @Override
//        public void onTick(long millis) {
//            if (isFinishing() || isDestroyed()){
//                return;
//            }
//            btn_check_code.setClickable(false);
//            btn_check_code.setText(String.format(register_vc_resend,millis/1000));
//        }
//
//        @Override
//        public void onFinish() {
//            if (isFinishing() || isDestroyed()){
//                return;
//            }
//            btn_check_code.setClickable(true);
//            btn_check_code.setText(R.string.register_get_vc);
//
//        }
//    };

    @OnClick(R.id.btn_help)
    protected void onHelpClick(){
        requestPhoneCall(tel);
    }

//    @Override
//    protected void onDestroy() {
//        mTimer.cancel();
//        mTimer = null;
//        super.onDestroy();
//    }

    private String tel;
    private void tel(){
        Observable<EResponse> observable = api().tel(new HashMap<String, String>(1));
        observer(observable, new EObserver() {
            @Override
            public void successful(EResponse response) {
                tel = response.string("list");
            }

            @Override
            public void failure(String msg) {

            }
        });
    }

    public static final int REQ_PHONE_CALL = 104;
    public  void requestPhoneCall(String phone){
        if(TextUtils.isEmpty(phone)) {
            toast(R.string.consult_phone_invalid);
            return;
        }

        if(ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE},REQ_PHONE_CALL);
            return;
        }
        phoneCall(phone);
    }

    public void phoneCall(String phone){
        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+phone));
        startActivity(intent);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQ_PHONE_CALL){
            if (grantResults.length >0){
            }
        }
    }
}
